<template>
  <div class="student-submission-card">
    <!-- 卡片头部 -->
    <div class="card-header">
      <div class="header-left">
        <h3 class="task-title">{{ record.title }}</h3>
        <div class="task-name">{{ record.taskName }}</div>
      </div>
      <div class="header-right">
        <div class="status-badge" :class="getStatusClass(record.status)">
          {{ getStatusText(record.status) }}
        </div>
      </div>
    </div>

    <!-- 卡片内容 -->
    <div class="card-content">
      <!-- 时间信息 -->
      <div class="time-info">
        <div class="time-item">
          <span class="time-label">提交时间：</span>
          <span class="time-value">{{ formatTime(record.submitTime) }}</span>
        </div>
        <div v-if="record.gradeTime" class="time-item">
          <span class="time-label">批改时间：</span>
          <span class="time-value">{{ formatTime(record.gradeTime) }}</span>
        </div>
      </div>

      <!-- 成绩信息 -->
      <div v-if="record.score !== null" class="score-info">
        <div class="score-display">
          <span class="score-value">{{ record.score }}</span>
          <span class="score-total">/ {{ record.totalScore }}</span>
        </div>
        <div class="score-percentage">
          {{ Math.round((record.score / record.totalScore) * 100) }}%
        </div>
      </div>
      <div v-else class="score-info pending">
        <div class="score-display">
          <span class="score-pending">待批改</span>
        </div>
      </div>

      <!-- 反馈信息 -->
      <div v-if="record.feedback" class="feedback-info">
        <div class="feedback-label">老师反馈：</div>
        <div class="feedback-content">{{ record.feedback }}</div>
      </div>

      <!-- 错题信息 -->
      <div v-if="record.hasErrors" class="error-info">
        <div class="error-badge">
          <el-icon><Warning /></el-icon>
          <span>{{ record.errorCount }}道错题</span>
        </div>
      </div>
    </div>

    <!-- 卡片操作 -->
    <div class="card-actions">
      <el-button type="primary" link size="small" @click="handleDetail">
        查看详情
      </el-button>
      
      <el-button 
        v-if="record.status === 'correction'" 
        type="warning" 
        link 
        size="small" 
        @click="handleResubmit"
      >
        重新提交
      </el-button>
      
      <el-button 
        v-if="record.hasErrors && (record.status === 'graded' || record.status === 'correction')" 
        type="danger" 
        link 
        size="small" 
        @click="handleViewErrors"
      >
        查看错题
      </el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElButton, ElIcon } from 'element-plus'
import { Warning } from '@element-plus/icons-vue'

// 定义props
interface SubmissionRecord {
  id: number
  title: string
  taskName: string
  subject: string
  status: string
  submitTime: Date
  gradeTime: Date | null
  score: number | null
  totalScore: number
  feedback: string | null
  hasErrors: boolean
  errorCount: number
}

interface Props {
  record: SubmissionRecord
}

const props = defineProps<Props>()

// 定义emits
const emit = defineEmits<{
  detail: [record: SubmissionRecord]
  resubmit: [record: SubmissionRecord]
}>()

// 获取状态样式类
const getStatusClass = (status: string) => {
  const statusMap: Record<string, string> = {
    'submitted': 'status-submitted',
    'graded': 'status-graded',
    'correction': 'status-correction',
    'corrected': 'status-corrected',
    'completed': 'status-completed'
  }
  return statusMap[status] || 'status-default'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    'submitted': '已提交',
    'graded': '已批改',
    'correction': '需订正',
    'corrected': '已订正',
    'completed': '已完成'
  }
  return statusMap[status] || status
}

// 格式化时间
const formatTime = (date: Date) => {
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 处理操作事件
const handleDetail = () => {
  emit('detail', props.record)
}

const handleResubmit = () => {
  emit('resubmit', props.record)
}

const handleViewErrors = () => {
  // 查看错题集
  console.log('查看错题集:', props.record)
}
</script>

<style scoped>
.student-submission-card {
  background: #fff;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
}

.student-submission-card:hover {
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border-color: #e0e0e0;
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
}

.header-left {
  flex: 1;
  min-width: 0;
}

.task-title {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.task-name {
  font-size: 13px;
  color: #909399;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-right {
  flex-shrink: 0;
  margin-left: 12px;
}

/* 状态标签 */
.status-badge {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.status-submitted {
  background: #e1f3ff;
  color: #409eff;
}

.status-graded {
  background: #f0f9ff;
  color: #67c23a;
}

.status-correction {
  background: #fdf6ec;
  color: #e6a23c;
}

.status-corrected {
  background: #f4f4f5;
  color: #909399;
}

.status-completed {
  background: #f0f9ff;
  color: #67c23a;
}

/* 卡片内容 */
.card-content {
  margin-bottom: 16px;
}

/* 时间信息 */
.time-info {
  margin-bottom: 12px;
}

.time-item {
  display: flex;
  align-items: center;
  margin-bottom: 4px;
  font-size: 13px;
}

.time-item:last-child {
  margin-bottom: 0;
}

.time-label {
  color: #909399;
  min-width: 70px;
}

.time-value {
  color: #606266;
}

/* 成绩信息 */
.score-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 6px;
}

.score-info.pending {
  justify-content: center;
}

.score-display {
  display: flex;
  align-items: baseline;
  gap: 2px;
}

.score-value {
  font-size: 24px;
  font-weight: 700;
  color: #409eff;
}

.score-total {
  font-size: 16px;
  color: #909399;
}

.score-pending {
  font-size: 14px;
  color: #909399;
}

.score-percentage {
  font-size: 14px;
  font-weight: 600;
  color: #67c23a;
}

/* 反馈信息 */
.feedback-info {
  margin-bottom: 12px;
}

.feedback-label {
  font-size: 13px;
  color: #909399;
  margin-bottom: 4px;
}

.feedback-content {
  font-size: 14px;
  color: #606266;
  line-height: 1.4;
  background: #fafafa;
  padding: 8px 12px;
  border-radius: 4px;
  border-left: 3px solid #409eff;
}

/* 错题信息 */
.error-info {
  margin-bottom: 12px;
}

.error-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  background: #fef0f0;
  color: #f56c6c;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

/* 卡片操作 */
.card-actions {
  display: flex;
  gap: 12px;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
}

.card-actions .el-button {
  padding: 0;
  font-size: 13px;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .student-submission-card {
    padding: 16px;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .header-right {
    margin-left: 0;
  }

  .task-title {
    font-size: 15px;
  }

  .score-value {
    font-size: 20px;
  }

  .card-actions {
    flex-wrap: wrap;
    gap: 8px;
  }
}
</style>
